//
// Created by Administrator on 2021/8/10.
//
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <climits>

using namespace std;

class Solution {
public:
    vector<string> findRestaurant(vector<string> &list1, vector<string> &list2) {
        unordered_map<string, int> hashList1;
        unordered_map<string, int> hashAns;
        int minIndexSum = INT_MAX;
        for (int i = 0; i < list1.size(); ++i) {
            hashList1[list1[i]] = i;
        }
        for (int i = 0; i < list2.size(); ++i) {
            auto str = list2[i];
            if (hashList1.count(str)) {
                hashAns[str] = hashList1[str] + i;
                minIndexSum = min(minIndexSum,hashAns[str] );
            }
        }
        vector<string> ans;
        for (auto &x:hashAns) {
            if (x.second == minIndexSum) {
                ans.push_back(x.first);
            }
        }
        return ans;
    }
};

int main() {
    vector<string> L1{"Shogun", "Tapioca Express", "Burger King", "KFC"};
    vector<string> L2{"Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"};
    Solution solution;
    auto ans = solution.findRestaurant(L1, L2);
    for (auto &x:ans)
        cout << x << endl;
    return 0;
}
